<?php
namespace Admin\Controller;
use Think\Controller;
class WxmessageController extends BaseController {
    
	public static $wx;//缓存微信对象
	protected function _initialize(){
		parent::_initialize();
		
		$options['appid']=C('BB_APPID_client');
		$options['secret']=C('BB_SECRET_client');
		self::$wx=new \bb\Wechat($options);		//new 微信类
	}
	//按人排序
	public function msglist(){
		$m=M('weixin_message');
		$user=M('weixin_client_user');
		$p=$_GET['p']?$_GET['p']:1;
		$psize=C('BB_PSIZE')?C('BB_PSIZE'):20;
		$offset=$psize*($p-1);
		$sql="SELECT * FROM (SELECT * FROM weixin_message where fromusername!='公众号回复' ORDER BY createtime DESC) BIAOMING GROUP BY fromusername ORDER BY createtime DESC LIMIT ".$offset.",".$psize;
		$cache=$m->query($sql);
		$count=count($cache);
		$this->getPage($count, $psize, 'bb-loader', '消息管理','bb-search');
		foreach ($cache as $k => $v){
			if(strlen($v['fromusername'])!=15){
				$u=$user->where(array('openid'=>$v['fromusername']))->find();
				$cache[$k]['nickname']=$u['nickname'];
				$cache[$k]['headimgurl']=$u['headimgurl'];
				$cache[$k]['uid']=$u['id'];
				$num=$m->where(array('fromusername'=>$v['fromusername'],'is_look'=>'0'))->count();
				$cache[$k]['num']=$num;
			}else{
				$cache[$k]['nickname']=$v['fromusername'];
				$cache[$k]['headimgurl']="/Public/admin/img/tx.png";
				$cache[$k]['is_img']="1";
			}
		}
		$this->assign('cache',$cache);
		$this->display();
	}
	
	//==========================
    public function index(){
    	$bread=array(
			'0'=>array(
				'name'=>'微信消息',
				'url'=>U('Admin/Wxmessage/index')
			),
			'1'=>array(
				'name'=>'消息管理',
				'url'=>U('Admin/Wxmessage/index')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
    		$m=M('weixin_message');
		$user=M('weixin_client_user');
		$p=$_GET['p']?$_GET['p']:1;
		$psize=C('BB_PSIZE')?C('BB_PSIZE'):20;
		$cache=$m->where($map)->page($p,$psize)->order('createtime desc')->select();
		$count=$m->where($map)->count();
		$this->getPage($count, $psize, 'bb-loader', '消息管理','bb-search');
		foreach ($cache as $k => $v) {
			if(strlen($v['fromusername'])!=15){
				$u=$user->where(array('openid'=>$v['fromusername']))->find();
				$cache[$k]['nickname']=$u['nickname'];
				$cache[$k]['headimgurl']=$u['headimgurl'];
				$cache[$k]['uid']=$u['id'];
			}else{
				$cache[$k]['nickname']=$v['fromusername'];
				$cache[$k]['headimgurl']="/Public/admin/img/tx.png";
				$cache[$k]['is_img']="1";
			}
		}
		$this->assign('cache',$cache);
		$this->display();
    }
	
	public function sendmsg(){
		$bread=array(
			'0'=>array(
				'name'=>'微信消息',
				'url'=>U('Admin/Wxmessage/index')
			),
			'1'=>array(
				'name'=>'消息管理',
				'url'=>U('Admin/Wxmessage/index')
			),
			'2'=>array(
				'name'=>'发送消息',
				'url'=>U('Admin/Wxmessage/sendmsg')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		$m=M('weixin_message');
		$user=M('weixin_client_user');
		$id=I('id');
		if(IS_POST){
			$p=I('post.');
			$re=self::$wx->sendMsg($p['openid'],$p['content']);
			if($re){
				$msgsend['tousername']=$p['openid'];
				$msgsend['fromusername']='公众号回复';
				$msgsend['createtime']=time();
				$msgsend['msgtype']='text';
				$msgsend['content']=$p['content'];
				$msgsend['is_look']=1;
				$m->add($msgsend);
				
				$info['status']=1;
				$info['msg']='发送成功！';
			}else{
				$info['status']=0;
				$info['msg']='发送失败！';
			}
			$this->ajaxreturn($info);
		}
		if($id){
			$openid=$user->where(array('id'=>$id))->getField('openid');
			if($openid){
				$this->assign('openid',$openid);
				
				$msg1=$m->where(array('fromusername'=>$openid))->order('createtime desc')->limit(40)->select();
				$msg2=$m->where(array('tousername'=>$openid))->order('createtime desc')->limit(40)->select();
				foreach ($msg1 as $k => $v) {
					$u=$user->where(array('openid'=>$v['fromusername']))->find();
					$msg1[$k]['nickname']=$u['nickname'];
					$msg1[$k]['headimgurl']=$u['headimgurl'];
					$up['id']=$v['id'];
					$up['is_look']=1;
					$m->save($up);
				}
				foreach ($msg2 as $k => $v) {
					//$u=$user->where(array('openid'=>$v['fromusername']))->find();
					$msg2[$k]['nickname']='挺赞洗衣';
					$msg2[$k]['headimgurl']='__PUBLIC__/admin/img/tingzan.png';
					$up['id']=$v['id'];
					$up['is_look']=1;
					$m->save($up);
				}
				$msg=array_merge($msg1,$msg2);
				//-排序-----------------------------------------
				$sort = array('direction' => 'SORT_ASC','field'=> 'createtime');  
				$arrSort = array();  
				foreach($msg AS $uniqid => $row){
					foreach($row AS $key=>$value){  $arrSort[$key][$uniqid] = $value;  }  
				}  
				if($sort['direction']){  
					array_multisort($arrSort[$sort['field']], constant($sort['direction']), $msg);  
				}
				$this->assign('cache',$msg);
			}else{
				echo '出错了!';
				exit();
			}
		}
		$this->display();
	}
	
	//---------------
	function getmsg(){
		$m=M('weixin_message');
		$user=M('weixin_client_user');
		if(IS_POST){
			$openid=I('openid');
			$msg1=$m->where(array('fromusername'=>$openid,'is_look'=>0))->order('createtime desc')->limit(20)->select();
			$msg2=$m->where(array('tousername'=>$openid,'is_look'=>0))->order('createtime desc')->limit(20)->select();
			if($msg1){
				foreach ($msg1 as $k => $v){
					$u=$user->where(array('openid'=>$v['fromusername']))->find();
					$msg1[$k]['nickname']=$u['nickname'];
					$msg1[$k]['headimgurl']=$u['headimgurl'];
					$msg1[$k]['data']=date("Y-m-d H:i:s",$v['createtime']);
					$up['id']=$v['id'];
					$up['is_look']=1;
					$m->save($up);
				}
			}
			if($msg2){
				foreach ($msg2 as $k => $v){
					$u=$user->where(array('openid'=>$v['fromusername']))->find();
					$msg2[$k]['nickname']=$u['nickname'];
					$msg2[$k]['headimgurl']=$u['headimgurl'];
					$msg2[$k]['data']=date("Y-m-d H:i:s",$v['createtime']);
					$up['id']=$v['id'];
					$up['is_look']=1;
					$m->save($up);
				}
			}
			if($msg1 || $msg2){
				$msg=array_merge($msg1,$msg2);
				//-排序-----------------------------------------
				$sort = array('direction' => 'SORT_ASC','field'=> 'createtime');  
				$arrSort = array();  
				foreach($msg AS $uniqid => $row){
					foreach($row AS $key=>$value){  $arrSort[$key][$uniqid] = $value;  }  
				}
				if($sort['direction']){  
					array_multisort($arrSort[$sort['field']], constant($sort['direction']), $msg);  
				}
			}
			
			
			if($msg){
				$info['status']=1;
				$info['data']=$msg;
				$this->ajaxreturn($info);
			}else{
				$info['status']=0;
				$this->ajaxreturn($info);
			}
		}else{
			exit('请使用post来获取数据！');
		}
	}
	
}